﻿#include "baseexception.h"
#include "src/config/globals.h"
#include "src/helper/filehelper.h"
BaseException::BaseException(const QString& code, const QString& msg)
    : m_msg(msg)
    , m_code(code)
{}

ErrorMap BaseException::parseErrorCode(const QString& csvPath)
{
    ErrorMap           m_map;
    int                sectionCount = 6;
    QList<QStringList> records      = FileHelper::readAllCsv(csvPath);

    for (int i = 1; i < records.size(); i++) {
        auto    record = records[i];
        QString code;
        QString msg;
        for (int j = 0; j < sectionCount; ++j) {

            if (record[j] == "") { code += "0"; }
            else {
                QStringList strs = record[j].split("_");
                msg += strs[0];
                code += strs[1];
            }
        }
        if (record[sectionCount] != "") { msg = record[sectionCount]; }
        m_map[code] = msg;
    }
    return m_map;
}

void BaseException::gennerateErrorCodeHFile(const QString& csvPath, const QString& targetPath)
{
    QStringList rows;
    rows << "#ifndef ERRORCODE_H"
         << "#define ERRORCODE_H\n\n";

    ErrorMap                 errors = parseErrorCode(csvPath);
    ErrorMap::const_iterator iter   = errors.constBegin();
    while (iter != errors.constEnd()) {
        rows << QString::fromLocal8Bit("#define EC_%1 \"%1\"  // %2").arg(iter.key(), iter.value());
        ++iter;
    }
    rows << "\n\n#endif // ERRORCODE_H";
    FileHelper::writeFile(rows, targetPath);
}

QString BaseException::msg() const
{
    static ErrorMap m_map = BaseException::parseErrorCode(GLOBAL::PATH::ERROR_CODE_PATH);
    QString         m     = QString::fromLocal8Bit("错误(%1): %2").arg(m_code, m_map[m_code]);
    if (m_msg != "") { m += QString::fromLocal8Bit("，详情: %1").arg(m_msg); }
    return m;
}

QString BaseException::code() const
{
    return QString();
}

BaseException::~BaseException() {}
